<?php

/**
 * @author Florian Garding
 */
/**
 * @ignore
 */
if (!defined('IN_PHPBB')) {
    exit;
}

/**
 * @package acp
 */
class acp_server {

    var $u_action;

    function main($id, $mode) {
        global $db, $user, $auth, $template;
        global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

        $action = request_var('action', '');
        $server_id = request_var('server_id', false);

        $this->tpl_name = 'acp_server';
        $this->page_title = 'Server';

        if ($_POST['add']) {
            $action = 'add';
        }

        switch ($action) {
            case 'move_up':
            case 'move_down':

                if (!$server_id) {
                    trigger_error('Keinen Server ausgew&auml;hlt' . adm_back_link(), E_USER_WARNING);
                }

                $sql = 'SELECT *
			FROM ' . SERVER_TABLE . "
			WHERE server_id = $server_id";
                $result = $db->sql_query($sql);
                $row = $db->sql_fetchrow($result);
                $db->sql_freeresult($result);

                if (!$row) {
                    trigger_error($user->lang['NO_FORUM'] . adm_back_link($this->u_action), E_USER_WARNING);
                }

                $move_server_name = $this->move_server_by($row, $action, 1);

                if ($move_server_name !== false) {
                    add_log('admin', 'LOG_FORUM_' . strtoupper($action), $row['forum_name'], $move_forum_name);
                    $cache->destroy('sql', SERVER_TABLE);
                }

                break;
            case 'activate':
            case 'deactivate':

                if (!$server_id) {
                    trigger_error('Keinen Server ausgew&auml;hlt' . adm_back_link($this->u_action), E_USER_WARNING);
                }

                $sql = 'UPDATE ' . SERVER_TABLE . ' SET active = ' . ($action == 'activate') ? 1 : 0 . ' WHERE server_id = ' . $server_id;
                $db->sql_query($sql);

                break;
            case 'delete':

                if (!$server_id) {
                    trigger_error('Keinen Server ausgew&auml;hlt' . adm_back_link($this->u_action), E_USER_WARNING);
                }

                $sql = 'DELETE FROM ' . SERVER_TABLE . ' WHERE server_id = ' . $server_id;
                $db->sql_query($sql);

                break;
            case 'edit':
            case 'add':

                if (!$server_id && $action == 'edit') {
                    trigger_error('Keinen Server ausgew&auml;hlt' . adm_back_link($this->u_action), E_USER_WARNING);
                }

                //Spiele auslesen!
                $gamesini = parse_ini_file($phpbb_root_path . 'GameQ/games.ini', true);
                foreach ($gamesini as $key => &$entry) {
                    $entry['id'] = $key;
                }

                $template->assign_var('SERVER_GAMES', $s_options);

                if ($action == 'edit') {
                    $sql = 'SELECT * FROM ' . SERVER_TABLE . '
                            WHERE server_id = ' . $server_id;
                    $result = $db->sql_query($sql);
                    $row = $db->sql_fetchrow($result);

                    $s_games = '';
                    foreach ($gamesini as $value) {
                        $selected = ($row['game'] == $value['id']) ? 'selected="selected" ' : '';
                        $s_games .= '<option ' . $selected . ($row['game'] == $value['id']) ? 'selected="selected" ' : '' . 'value="' . $value['id'] . '">' . $value['name'] . '</option>';
                    }

                    $template->assign_vars(array(
                        'SERVER_NAME' => $row['server_name'],
                        'SERVER_GAMES' => $s_games,
                        'SERVER_IPPORT' => $row['ipport'],
                        'SERVER_IP' => $row['ip'],
                        'SERVER_PORT' => $row['port'],
                        'U_ACTION' => $this->u_action . '&amp;server_id=' . $server_id . '&amp;action=' . $action,
                        'U_BACK' => $this->u_action,
                        'S_EDIT_SERVER' => true)
                    );
                    $db->sql_freeresult($result);
                } else {
                    $s_games = '';
                    foreach ($gamesini as $value) {
                        $s_games .= '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
                    }

                    $template->assign_vars(array(
                        'SERVER_NAME' => '',
                        'SERVER_GAMES' => $s_games,
                        'SERVER_IPPORT' => '',
                        'SERVER_IP' => '',
                        'SERVER_PORT' => '',
                        'U_ACTION' => $this->u_action . '&amp;server_id=' . $server_id . '&amp;action=' . $action,
                        'U_BACK' => $this->u_action,
                        'S_EDIT_SERVER' => true)
                    );
                    $db->sql_freeresult($result);
                }

                if ($submit) {

                }
                break;
            default:
                break;
        }

        $template->assign_vars(array(
            'U_ACTION' => $this->u_action,
            'SERVER_GAMES' => $s_options)
        );

        $sql = 'SELECT * FROM ' . SERVER_TABLE . '
                ORDER BY left_id ASC';
        $result = $db->sql_query($sql);

        while ($row = $db->sql_fetchrow($result)) {
            $template->assign_block_vars('server', array(
                'SERVER_NAME' => $row['server_name'],
                'SERVER_GAME' => $row['game'],
                'U_EDIT' => $this->u_action . '&amp;server_id=' . $row['server_id'] . '&amp;action=edit',
                'U_DELETE' => $this->u_action . '&amp;server_id=' . $row['server_id'] . '&amp;action=delete',
                'U_ACTIVATE' => $this->u_action . '&amp;server_id=' . $row['server_id'] . '&amp;action=' . ($row['active'] == 1) ? 'deactivate' : 'activate')
            );
        }
        $db->sql_freeresult($result);
    }

    /**
     * Move server position by $steps up/down
     */
    function move_server_by($server_row, $action = 'move_up', $steps = 1) {
        global $db;

        /**
         * Fetch all the siblings between the module's current spot
         * and where we want to move it to. If there are less than $steps
         * siblings between the current spot and the target then the
         * module will move as far as possible
         */
        $sql = 'SELECT server_id, server_name, left_id, right_id
			FROM ' . Server_TABLE . "
			WHERE " . (($action == 'move_up') ? "right_id < {$server_row['right_id']} ORDER BY right_id DESC" : "left_id > {$server_row['left_id']} ORDER BY left_id ASC");
        $result = $db->sql_query_limit($sql, $steps);

        $target = array();
        while ($row = $db->sql_fetchrow($result)) {
            $target = $row;
        }
        $db->sql_freeresult($result);

        if (!sizeof($target)) {
            // The server is already on top or bottom
            return false;
        }

        /**
         * $left_id and $right_id define the scope of the nodes that are affected by the move.
         * $diff_up and $diff_down are the values to substract or add to each node's left_id
         * and right_id in order to move them up or down.
         * $move_up_left and $move_up_right define the scope of the nodes that are moving
         * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
         */
        if ($action == 'move_up') {
            $left_id = $target['left_id'];
            $right_id = $server_row['right_id'];

            $diff_up = $server_row['left_id'] - $target['left_id'];
            $diff_down = $server_row['right_id'] + 1 - $forum_row['left_id'];

            $move_up_left = $server_row['left_id'];
            $move_up_right = $server_row['right_id'];
        } else {
            $left_id = $server_row['left_id'];
            $right_id = $target['right_id'];

            $diff_up = $server_row['right_id'] + 1 - $server_row['left_id'];
            $diff_down = $target['right_id'] - $server_row['right_id'];

            $move_up_left = $server_row['right_id'] + 1;
            $move_up_right = $target['right_id'];
        }

        // Now do the dirty job
        $sql = 'UPDATE ' . SERVER_TABLE . "
			SET left_id = left_id + CASE
				WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
				ELSE {$diff_down}
			END,
			right_id = right_id + CASE
				WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
				ELSE {$diff_down}
			END";
//                        forum_parents = ''
//			WHERE
//				left_id BETWEEN {$left_id} AND {$right_id}
//				AND right_id BETWEEN {$left_id} AND {$right_id}";
        $db->sql_query($sql);

        return $target['server_name'];
    }

}

?>